/*
 * This file is part of mmoMinecraft (http://code.google.com/p/mmo-minecraft/).
 * 
 * mmoMinecraft is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.

 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package mmo.Core;

import org.getspout.spoutapi.gui.Color;
import org.getspout.spoutapi.gui.ContainerType;
import org.getspout.spoutapi.gui.GenericGradient;
import org.getspout.spoutapi.gui.GenericLabel;
import org.getspout.spoutapi.gui.Label;

public class HealthBar extends GenericContainer {

	private GenericContainer _bars;
	private Label _label;
	private GenericGradient _health;
	private GenericGradient _armor;
	private int health = 0;
	private int armor = 0;
	String label = "";

	public HealthBar() {
		super();
		Color red = new Color(1f, 0, 0);
		Color gray = new Color(0.5f, 0.5f, 0.5f);

		this.setLayout(ContainerType.OVERLAY).setFixed(true);
		this.addChild(_bars = new GenericContainer());
		this.addChild(_label = new GenericLabel());
		_bars.setLayout(ContainerType.VERTICAL);
		_bars.addChild(_health = new GenericGradient());
		_bars.addChild(_armor = new GenericGradient());
		_label.setText("");
		_health.setTopColor(red).setBottomColor(red);
		_armor.setTopColor(gray).setBottomColor(gray);
		
		this.setWidth(64).setHeight(14); // Don't touch the size again, just the location
	}

	public HealthBar setHealth(int health) {
		if (this.health != health) {
			this.health = health;
			System.out.println("Width: " + this.getWidth());
			System.out.println("Health: " + health + " = " + ((this.getWidth() * health) / 100));
			_health.setWidth((this.getWidth() * health) / 100);
			_health.setDirty(true);
		}
		return this;
	}

	public HealthBar setArmor(int armor) {
		if (this.armor != armor) {
			this.armor = armor;
			System.out.println("Armor: " + armor + " = " + ((this.getWidth() * armor) / 100));
			_armor.setWidth((this.getWidth() * armor) / 100);
			_armor.setDirty(true);
		}
		return this;
	}

	public HealthBar setLabel(String label) {
		if (!this.label.equals(label)) {
			this.label = label;
			_label.setText(label);
			_label.setDirty(true);
		}
		return this;
	}
}
